﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Reflection;

namespace RiderShop.Mobile.Common.Component
{
    /// <summary>
    /// HTML 扩展控件
    /// </summary>
    public static partial class HtmlExtension
    {
        public const string StrAllTemplete = @"
        <a href='{0}{1}{2}' data-role='button' class='btn btn-success'><i class='glyphicon glyphicon-chevron-left'></i>上一页</a> 
        <a href='{3}{4}{5}' data-role='button' class='btn btn-success'>下一页<i class='glyphicon glyphicon-chevron-right'></i></a>
";
        public const string StrNoPreviousTemplete = @"
        <span class='btn btn-success' disabled='disabled'><i class='glyphicon glyphicon-chevron-left'></i>上一页</span>
        <a href='{0}{1}{2}' data-role='button' class='btn btn-success'>下一页<i class='glyphicon glyphicon-chevron-right'></i></a>
";
        public const string StrNoNextTemplete = @"
        <a href='{0}{1}{2}' data-role='button' class='btn btn-success'><i class='glyphicon glyphicon-chevron-left'></i>上一页</a> 
        <span class='btn btn-success' disabled='disabled'>下一页<i class='glyphicon glyphicon-chevron-right'></i></span>
";
        #region 分页控件
        /// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="PageSize">单页条数</param>
        /// <param name="CurrentPage">当前页</param>
        /// <param name="RecordCount">总记录数</param>
        /// <param name="Attribute">扩展参数</param>
        /// <returns></returns>
        public static MvcHtmlString Pagination(this HtmlHelper helper, Int32 PageSize, Int32 CurrentPage, Int32 RecordCount, Object Attribute)
        {
            /*
             * <span>共：{RecordCount}条 {PageSize}条/页 {CurrentPage}页/{PageCount}页</span> {List}
             * <span>Page {CurrentPage} of {PageCount} ({RecordCount} items) PageSize：{PageSize}</span> {List}
             * @ViewType = 0,   0中文 1英文 2自定义中文 3自定义英文
             * @Template = "<span>共：{RecordCount}条 {PageSize}条/页 {CurrentPage}页/{PageCount}页</span> {List}", 
             * @Postfix = "", 
             * @VirtualPath = "", 
             * @Identifier = "page", 
             * @Encrypt = false }
            */

            Int32 ViewType = GetInt32(GetValue(Attribute, "ViewType"));
            string Template = GetString(GetValue(Attribute, "Template"));
            //String Postfix = GetString(GetValue(Attribute, "Postfix"));
            string VirtualPath = GetString(GetValue(Attribute, "VirtualPath"));
            string Identifier = GetString(GetValue(Attribute, "Identifier"));
            if (string.IsNullOrEmpty(Identifier))
            {
                Identifier = "page";
            }
            double divide = RecordCount * 1.0 / PageSize;
            double ceiled = Math.Ceiling(divide);
            Int32 pagecount = (Int32)ceiled;
            Int32 startPage = 0;
            Int32 endPage = 0;

            StringBuilder html = new StringBuilder();
            String CurrUrl = "";

            if (ViewType == 0 || ViewType == 1)
            {
                CurrUrl = DeleteUrlParameter(Identifier);
                Identifier = Identifier + "=";
            }
            else
            {
                CurrUrl = VirtualPath;
                Identifier = "";
            }

            if (pagecount <= 1)
            {
                return MvcHtmlString.Create(html.ToString());
            }
            if (CurrentPage == 0)
            {
                html.AppendFormat(StrNoPreviousTemplete,
                    CurrUrl,
                    Identifier,
                    CurrentPage + 1
                    );
            }
            else if (CurrentPage > 0 && CurrentPage == pagecount - 1)
            {
                html.AppendFormat(StrNoNextTemplete,
                     CurrUrl,
                     Identifier,
                     CurrentPage - 1
                     );
            }

            if (CurrentPage > 0 && CurrentPage < pagecount - 1)
            {
                html.AppendFormat(StrAllTemplete,
                    CurrUrl,
                    Identifier,
                    CurrentPage - 1,
                    CurrUrl,
                    Identifier,
                    CurrentPage + 1
                    );
            }
            return MvcHtmlString.Create(html.ToString());
        }

        #endregion

        #region 通用方法

        /// <summary>
        /// 转成String类型
        /// </summary>
        /// <param name="V"></param>
        /// <returns></returns>
        public static String GetString(Object V)
        {
            return GetString(V, String.Empty);

        }
        /// <summary>
        /// 转成String类型
        /// </summary>
        /// <param name="V"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static String GetString(Object V, String defaultValue)
        {
            if (!IsNull(V))
            {
                return V.ToString().Trim().Replace(">", "&gt;").Replace("<", "&lt;").Replace("'", "&#39;");
            }
            return defaultValue;
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="V"></param>
        /// <returns></returns>
        public static bool IsNull(Object V)
        {
            if (!((V == null) || Convert.IsDBNull(V)))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 转成Int32类型
        /// </summary>
        /// <param name="V"></param>
        /// <returns></returns>
        public static Int32 GetInt32(Object V)
        {
            return GetInt32(V, 0);
        }
        /// <summary>
        /// 转成Int32类型
        /// </summary>
        /// <param name="V"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Int32 GetInt32(Object V, Int32 defaultValue)
        {
            if (!(!IsNull(V) && IsInt32(V)))
            {
                return defaultValue;
            }
            return Convert.ToInt32(V);
        }

        /// <summary>
        /// 是否为Int32类型
        /// </summary>
        /// <param name="V"></param>
        /// <returns></returns>
        public static bool IsInt32(Object V)
        {
            try
            {
                Convert.ToInt32(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 转成Boolean类型
        /// </summary>
        /// <param name="V"></param>
        /// <returns></returns>
        public static Boolean GetBoolean(Object V)
        {
            return GetBoolean(V, false);
        }
        /// <summary>
        /// 转成Boolean类型
        /// </summary>
        /// <param name="V"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Boolean GetBoolean(Object V, Boolean defaultValue)
        {
            if (!(!IsNull(V) && IsBoolean(V)))
            {
                return defaultValue;
            }
            return Convert.ToBoolean(V);
        }
        /// <summary>
        /// 是否为Boolean类型
        /// </summary>
        /// <param name="V"></param>
        /// <returns></returns>
        public static Boolean IsBoolean(Object V)
        {
            try
            {
                Convert.ToBoolean(V);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 转成String类型
        /// </summary>
        /// <param name="V"></param>
        /// <returns></returns>
        public static String SetString(Object V)
        {
            if (!IsNull(V))
            {
                return V.ToString().Trim().Replace("&gt;", ">").Replace("&lt;", "<").Replace("&#39;", "'");
            }
            return "";
        }
        /// <summary>
        /// 获取扩展参数 { @key = value,@keys = values }
        /// </summary>
        /// <param name="ob"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static String GetValue(Object ob, String key)
        {
            if (ob == null)
            {
                return string.Empty;
            }
            String tx = string.Empty;
            foreach (PropertyInfo pi in ob.GetType().GetProperties())
            {
                String title = pi.Name;
                if (title == key)
                {
                    tx = pi.GetValue(ob, null).ToString();
                    break;
                }
            }
            return tx;
        }


        /// <summary>
        /// 删除页面参数
        /// </summary>
        /// <param name="P">参数名称</param>
        /// <returns></returns>
        public static String DeleteUrlParameter(String P)
        {
            String S = System.Web.HttpContext.Current.Request.RawUrl;
            S = Regex.Replace(S, @"(?<=.*?\?.+?)&" + P + @"=\d+|(?<=.*?\?)" + P + @"=\d+&|(?<=.*?)\?" + P + @"=\d+$", "");
            return Regex.IsMatch(S, @"\?/g") ? S + "?" : Regex.IsMatch(S, @"(&|\?)") ? S + "&" : S + "?";
        }
        #endregion
    }
}